﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：庄金冬 
    //
    // 文件名：DAL_User_Role.cs
    // 文件功能描述：用户角色数据访问逻辑
    // 创建标识：庄金冬2009-03-06
    //
    // 修改标识：周司徒2009-03-09
    // 修改描述：实现GetAllUserByRole，GetChildUserByUserID
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/

using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade.Model;
using System.Data;
using System.Data.SqlClient;


namespace HZ.DrugTrade.DAL
{
    /// <summary>
    /// 用户角色数据访问逻辑
    /// </summary>
    public class DAL_User_Role
    {

        /// <summary>
        /// 通过角色获取用户
        /// </summary>
        /// <param name="role">角色</param>
        /// <returns></returns>
        public static IList<User> GetAllUserByRole(Role role)
        {
            string sql_GetUserByRole = "SELECT tu.*,tr.* FROM TB_User AS tu INNER JOIN  TB_Role_User AS tru ON tu.UserID = tru.UserID INNER JOIN  TB_Role AS tr ON tru.RoleID = tr.RoleID WHERE {0}";
            StringBuilder where = new StringBuilder("1=1");
            if (!string.IsNullOrEmpty(role.RoleID))
            {
                where.AppendFormat(" AND tr.RoleID = '{0}'", role.RoleID);
            }
            //if (!string.IsNullOrEmpty(role.RoleName))
            //{
            //    where.AppendFormat(" AND tr.RoleName LIKE '%{0}%'", role.RoleName);
            //}
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(string.Format(sql_GetUserByRole, where.ToString()));

            //return HZ.Data.DAL_Helper.GetUsersFromDataTable(dt);
            return HZ.Data.DAL_Helper.CommonFillList<User>(dt);
        }
        /// <summary>
        /// 获取子用户
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static  IList<User> GetChildUserByUserID(string userid)
        {
            string sql_GetChildUserByUserID = @"SELECT * FROM TB_User AS tu INNER JOIN  TB_Role_User AS tru ON tu.UserID = tru.UserID INNER JOIN  TB_User AS tu2 ON tru.UserID = tu2.UserID WHERE tru.IsAdmin = 0 AND tru.UserID = '{0}'";
            //return HZ.Data.DAL_Helper.GetUsersFromDataTable(HZ.Data.DbHelperFactory.Create().ExecuteTable(string.Format(sql_GetChildUserByUserID, userid)));
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(string.Format(sql_GetChildUserByUserID, userid));
            return HZ.Data.DAL_Helper.CommonFillList<User>(dt);
        }
    }
}
